#usr/bin/python

from PyQt4 import QtGui, QtCore
from core import clustering as clust, pySpec as obj
from model import clustering_peaks
from controller import ControllerDialog as dial

class cluster_controller(dial):
	"""Class to Control the gui and the model for clustering"""
	
	def __init__(self, lspl, visu):
		"""
		constructor
		"""
		dial.__init__(self, lspl, visu)
		
		self.visu.initialize()
		
		QtCore.QObject.connect(self.visu.pushButton, QtCore.SIGNAL("clicked()"),self.printing)
		QtCore.QObject.connect(self.visu.pushButton_2, QtCore.SIGNAL("clicked()"),self.OpenFile)
		QtCore.QObject.connect(self.visu.buttonBox, QtCore.SIGNAL("accepted()"),self.Clustering)

	
	def Clustering(self):
		"""Main Function Clustering"""
		
		#if self.visu.parent().pb.value():
		#	self.visu.parent().setValue(0)
		
		#get files to treat
		filename_to_process = self.visu.lineEdit.text().split(';')
		
		#get parameters values
		M0_mode =False; frag_mode =False; use_correlation =False
		error_rt =float(self.visu.lineEdit_2.text())
		iso_length =int(self.visu.lineEdit_3.text())
		idms_length = int(self.visu.lineEdit_4.text())
		
		if self.visu.checkBox.isChecked():
			use_correlation = True
			corr = float(self.visu.lineEdit_5.text())
		if self.visu.radioButton.isChecked():
			M0_mode =True
		elif self.visu.radioButton_2.isChecked():
			frag_mode=True
		self.visu.close()
		
		lspl = obj.ListSample()
		for xml in filename_to_process:
			if xml not in self.lspl.getFiles():
				print ("error, must parse before")
				return
			lspl.append(self.lspl.Sample(xml))
		
		#for spl in lspl:
			"""
			WITHOUT THREADing
			cl = clustering_peaks.identification_clusters(spl, self.lspl)
			cl.clusters(error_rt, iso_length, idms_length, use_correlation, corr)
			#cl.print_res()
			cl.build_models(self.visu.parent().clust_model,  spl.get_peak_model(),  spl.get_clust_model())
			self.visu.parent().treeView_3.setModel(self.visu.parent().clust_model)
			
			table_view= QtGui.QTableView()
			table_view.setModel(spl.get_clust_model())
			table_view.setSortingEnabled(True)

			self.visu.parent().addMdiSubWindow(table_view)
			
			table_view2= QtGui.QTableView()
			table_view2.setModel(spl.get_peak_model())
			table_view2.setSortingEnabled(True)

			self.visu.parent().addMdiSubWindow(table_view2)
			"""
			#print "type parent", type(self.visu.parentWidget())
		self.visu.parent().ShowInStatusBar("Clustering Step...Please Wait..",  5000)
		a = clust_thread(lspl, error_rt, iso_length, idms_length, use_correlation, corr, self.visu.parent())
		#QtCore.QObject.connect(a, QtCore.SIGNAL("done"),self.finished)
		#QtCore.QObject.connect(a, QtCore.SIGNAL("upadte_pb"),self.visu.parent().update_progress_bar)
		QtCore.QObject.connect(a, QtCore.SIGNAL("started()"),self.visu.parent().to_indetermined_mode)
		QtCore.QObject.connect(a, QtCore.SIGNAL("finished()"),self.visu.parent().to_determined_mode)
		QtCore.QObject.connect(a, QtCore.SIGNAL("finished()"),self.done)
		a.begin()
			
	def done(self):
		#self.visu.parent().clust_model = model
		self.visu.parent().treeView_3.setModel(self.visu.parent().clust_model)
		self.visu.parent().ShowInformationMessage("Finished", "clustering done sucessfully !")

class clust_thread(QtCore.QThread):
	"""Threading Clustering process"""
	
	def __init__(self, lspl, error_rt, iso_length, idms_length, use_correlation, corr, main =None):
		"""Constructor"""
		
		QtCore.QThread.__init__(self)
		#self.spl =spl
		self.lspl =lspl
		self.error_rt =error_rt
		self.iso_length =iso_length
		self.idms_length =idms_length
		self.use_corr =use_correlation
		self.corr =corr
		self.main = main
		self.model = self.main.clust_model
		
	def run(self):
		"""have to implement this function"""
		
		for spl in self.lspl:
			cl = clustering_peaks.identification_clusters(spl, self.lspl)
			cl.clusters(self.error_rt, self.iso_length, self.idms_length, self.use_corr, self.corr)
			#cl.print_res()
			#self.connect(cl, QtCore.SIGNAL("update_pb"), self.emission)
			cl.build_models(self.model, spl.get_peak_model(), spl.get_clust_model())
		#self.emit(QtCore.SIGNAL("done"), self.model)
		

	
	def begin(self):
		"""
		launches the thread
		"""
		
		self.start()
		self.exec_()
